<?php
namespace Crm\Api;

use PhalApi\Api;
use Auth\Api\Token as Token;
use Crm\Domain\Mall as DomainMall;
use Crm\Domain\Score as DomainScore;
use Crm\Domain\WxUser as DomainWxUser;

/**
 * 积分服务类
 *
 * @author: luodong <751450467@qq.com> 2020-06-24
 */

class Score extends Api {

    /**
     * token对象
     */
    public $token;

    /**
     * 当前用户信息
     */
    public $user;

    public function __construct()
    {
        //允许所有域名发起的跨域请求
        header('Access-Control-Allow-Origin: *');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
        //header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
        //header('Access-Control-Allow-Headers:Authorization,content-type');  //响应头 请按照自己需求添加。
        //$this->di = \PhalApi\DI();
        $this->token = new Token();

    }

	public function getRules() {
        return array(
            'initialize' => array(
                'user_id'=> array('name' => 'user_id', 'type'=>'int','default'=>0,'require'=>false,'desc'=>'用户id'),
                'mall_id'=> array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID')
            ),
            'rule' => array(
                'mall_id'=> array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID')
            ),
            'sign' => array(
                'mall_id'=> array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID'),
                'series_num'=> array('name' => 'series_num', 'type'=>'int','require'=>true,'desc'=>'连续签到天数设置')
            ),

            'scanSign'=> array(
                'mall_id'=> array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID'),
                'scan'=> array('name' => 'scan', 'type'=>'string','require'=>true,'desc'=>'二维码内容(包括项目project_id,f_uid推荐二维码的工作人员ID)')
            ),
            'scoreFlagUpdate'=>array(
                'score_id'=> array('name' => 'score_id', 'type'=>'int','require'=>true,'desc'=>'积分ID'),
                'flag'=> array('name' => 'flag', 'type'=>'enum', 'range' => array('1','0'),'require'=>true,'desc'=>'Flag状态')
            ),

            'obtainScore'=>array(
                'mall_id'=> array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID'),
                'code'=> array('name' => 'code', 'type'=>'int','require'=>true,'desc'=>'积分类型代码,2020301签到,2020302邀请好友,2020303推荐好友,2020304愚公移山,2020305观看美文,到2020306分享美文,2020307参加活动,2020308完善资料,2020309注册,2020310邀请参与活动,2020311分享楼盘'),
                'action'=> array('name' => 'action', 'type'=>'string','require'=>true,'desc'=>'主体名称'),
                'action_id'=> array('name' => 'action_id', 'type'=>'int','require'=>true,'desc'=>'主体Id')
            ),
            'obtainInviteScore'=>array(
                'user_id'=>array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'得分用户ID'),
                'mall_id'=> array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID'),
                'code'=> array('name' => 'code', 'type'=>'int','require'=>true,'desc'=>'积分类型代码,2020301签到,2020302邀请好友,2020303推荐好友,2020304愚公移山,2020305观看美文,到2020306分享美文,2020307参加活动,2020308完善资料,2020309注册,2020310邀请参与活动,2020311分享楼盘'),
                'action'=> array('name' => 'action', 'type'=>'string','require'=>true,'desc'=>'主体名称'),
                'action_id'=> array('name' => 'action_id', 'type'=>'int','require'=>true,'desc'=>'主体Id')
            ),
            'consume' => array(
                'mall_id'   => array('name' => 'mall_id', 'type'=>'int','require'=>true,'desc'=>'默认商城ID'),
                'shop_id' 	=> array('name' => 'shop_id', 'type'=>'int','require'=>true,'desc'=>'商家ID'),
                'score' 	=> array('name' => 'score', 'type'=>'int','require'=>true,'desc'=>'消费积分')
            ),

        );
	}

    /**
     * 初始化积分页面
     * @desc 根据商城id,初始化积分页面
     * @return array config 积分页面配置信息
     * @return int time 当前时间戳
     */
    public function initialize(){
        $domainMall = new DomainMall();
        $domainWxUser = new DomainWxUser();
        $domainScore = new DomainScore();
        $data['dayOfWeek'] = $domainScore->dayOfWeek;
        $data['weeks'] = $domainScore->weekSignList($this->user_id);
        $data['isSign'] = $domainScore->isSign($this->user_id);//用户今日是否签到
        $data['scanSignInfo'] = $domainScore->scanSignInfo($this->user_id);//用户今日是否现场签到
        $data['seriesNum'] = 3;//设置联系签到天数
        $data['seriesSignNum'] = $domainWxUser->seriesSignNum($this->user_id);//用户当前连续签到天数
        $data['setting']= $domainMall->setting($this->mall_id,array('code'=>'2020402'),'keylist');//获取积分页面配置
        return $data;
    }
	
	/**
	 * 获取积分配置
     * @desc 根据商城id,获取积分配置
     * @return array setting 积分配置
	 * @return int time 当前时间戳
	 */
	public function rule() {
        $domain = new DomainScore();
        $param = \PhalApi\DI()->request->getAll();
        $rs = $domain->rule($this->mall_id,$param);
        return $rs;
	}

    /**
     * 用户签到
     * @desc 用户线上签到
     * @return array result 签到结果
     * @return int time 当前时间戳
     */
    public function sign() {
        $this->user = $this->token->checkToken();
        $domain = new DomainScore();
        $rs = $domain->sign($this->user['user_id'],$this->mall_id,$this->series_num,$this->user['user_type']);
        return $rs;
    }

    /**
     * 扫码签到
     * @desc 用户扫码签到
     * @return array result 扫码签到结果
     * @return int time 当前时间戳
     */
    public function scanSign() {
        $this->user = $this->token->checkToken();
        $domain = new DomainScore();
        //5220,714(project_id,f_uid提供二维码工作人员id)
        $scan = $this->scan;
        $scanArr = explode(',',$scan);
        $rs = $domain->scanSign($this->user['user_id'],$this->mall_id,$scanArr[0],$scanArr[1],$this->user['user_type']);
        return $rs;
    }

    /**
     * 用户获得积分
     * @desc 规则说明:需要先验证文章是否已经阅读,其次根据阅读积分规则验证[如:每日只能获得3次阅读积分]
     * @return int result 获得积分结果
     * @return int time 当前时间戳
     */
    public function obtainScore(){
        $this->user = $this->token->checkToken();
        $domain = new DomainScore();
        //规则验证,是否可以获得积分
        $isObtain = $domain->checkObtainScore($this->user['user_id'],$this->code,$this->action,$this->action_id,$this->mall_id);
        $isRule = $domain->checkObtainRule($this->user['user_id'],$this->code,$this->action,$this->action_id,$this->mall_id);
        if ($isObtain && $isRule) {
            $rlt = $domain->addScore(
                $this->user['user_id'],
                $this->code,
                $this->action,
                $this->action_id,
                $this->mall_id,
                $this->user['user_type']
            );
        } else {
            //2020301签到,2020302邀请好友,2020303推荐好友,2020304愚公移山,2020305观看美文,到2020306分享美文,2020307参加活动,2020308完善资料,2020309注册,2020310邀请参与活动,2020311分享楼盘
            if($this->code=='2020305'){
                if(!$isObtain){
                    $rlt = array('ret' => '201', 'msg' => '您已获得该阅读积分');
                }else if(!$isRule){
                    $rlt = array('ret' => '202', 'msg' => '今日不能再获得积分');
                }else{
                    $rlt = array('ret' => '200', 'msg' => '您已获得该积分');
                }
            }elseif($this->code=='2020306'){
                if(!$isRule){
                    $rlt = array('ret' => '202', 'msg' => '今日不能再获得积分');
                }else{
                    $rlt = array('ret' => '201', 'msg' => '您已获得该分享积分');
                }
            }else{
                $rlt = array('ret' => '201', 'msg' => '您已获得该积分');
            }
        }
        return $rlt;
    }

    /**
     * 用户获得邀请积分
     * @desc 规则说明:需要先验证文章是否已经阅读,其次根据阅读积分规则验证[如:每日只能获得3次阅读积分]
     * @return int result 获得积分结果
     * @return int time 当前时间戳
     */
    public function obtainInviteScore(){
        $domain = new DomainScore();
        //规则验证,是否可以获得积分
        $isObtain = $domain->checkObtainScore($this->user_id,$this->code,$this->action,$this->action_id,$this->mall_id);
        if ($isObtain) {
            $rlt = $domain->addScore(
                $this->user['user_id'],
                $this->code,
                $this->action,
                $this->action_id,
                $this->mall_id,
                $this->user['user_type']
            );
        } else {
            $rlt = array('ret' => '200', 'msg' => '您已获得该积分!');
        }
        return $rlt;
    }

    /**
     * 用户积分Flag状态更新
     * @desc 用户积分Flag状态更新
     * @return int result 用户积分Flag状态更新结果
     * @return int time 当前时间戳
     */
    public function scoreFlagUpdate(){
        $this->user = $this->token->checkToken();
        $domain = new DomainScore();
        $rs = $domain->scoreFlagUpdate($this->user['user_id'],$this->score_id,$this->flag);
        return $rs;
    }

    /**
     * 积分消费
     * @desc 积分消费
     * @return obj Detail 积分消费结果
     * @return int time 当前时间戳
     */
    public function consume(){
        $this->user = $this->token->checkToken();
        $domain = new DomainScore();
        $data= $domain->consume($this->user['user_id'],$this->mall_id,$this->shop_id,$this->score);
        return $data;
    }

}
